package com.css.dzj.dzjap.modules.todolist.service.impl;

import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.common.util.ServiceUtil;
import com.css.dzj.dzjap.modules.record.approject.entity .QApRecordEntity;
import com.css.dzj.dzjap.modules.todolist.entity.ApTodoEntity;
import com.css.dzj.dzjap.modules.todolist.entity.QApTodoEntity;
import com.css.dzj.dzjap.modules.todolist.mapper.ApTodoMapper;
import com.css.dzj.dzjap.modules.todolist.service.ApTodoService;
import com.css.dzj.dzjap.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 待办事项service实现类
 */
@Service
public class ApTodoServiceImpl implements ApTodoService {

    @Autowired
    private ApTodoMapper apTodoMapper;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    private ServiceUtil serviceUtil;

    @Override
    public QueryResults<ApTodoEntity> getPage(String title, String status, String station, String createPerson, Pageable pageable, HttpServletRequest request) {
        /**
         * 获取suser
         */
        R r = serviceUtil.getUserInfoByRequest(request);
        String suserName = "";
        String location = "";
        String address="";
        String userUnit ="";
        List<String> stationList=new ArrayList<String>();
        if(r.getCode().equals("200")){
            LoginUser userInfo =  (LoginUser)r.get("data");
            /**
             * 获取用户名
             */
            suserName =userInfo.getLoginName();
            /**
             * 获取用户的省份
             */
            String orgName = userInfo.getOrgName();
           // location = orgName.substring(0,orgName.indexOf("地震局"));
            location = orgName.replace("地震局","");
            address = userInfo.getAddress();
            userUnit= userInfo.getOrgName();
            /*
                获取用户角色
             */
            stationList=userInfo.getRole();
        }
        QApTodoEntity q = QApTodoEntity.apTodoEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());
        predicate = (StringUtils.isEmpty(title)) ? predicate : ExpressionUtils.and(predicate, q.title.like("%" + title + "%"));
        predicate = (StringUtils.isEmpty(status)) ? predicate : ExpressionUtils.and(predicate, q.status.eq(status));
        predicate = (stationList==null) ? predicate : ExpressionUtils.and(predicate, q.station.in(stationList));
        predicate = (StringUtils.isEmpty(createPerson)) ? predicate : ExpressionUtils.and(predicate, q.createPerson.like("%" + createPerson + "%"));
        predicate = ExpressionUtils.and(predicate, q.delFlag.eq(Constant.NO_DELETE));
        predicate = ExpressionUtils.and(predicate,q.location.like("%" + location + "%"));
        return jpaQueryFactory
                .selectFrom(q)
                .where(predicate)
                .orderBy(
                        // 根据创建日期，降序排序
                        q.createTime.desc()
                )
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }

    @Override
    public ApTodoEntity findByLinkId(String linkId) {
        QApTodoEntity qApTodoEntity=QApTodoEntity.apTodoEntity;

        return jpaQueryFactory.selectFrom(qApTodoEntity).where(qApTodoEntity.linkId.eq(linkId)).fetchOne();
    }

    @Override
    public ApTodoEntity findByIdandType(String id, String typeFlag) {
        ApTodoEntity apTodoEntity= new ApTodoEntity();
        QApTodoEntity q = QApTodoEntity.apTodoEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());
        predicate = ExpressionUtils.and(predicate, q.delFlag.eq(Constant.NO_DELETE));
        predicate = ExpressionUtils.and(predicate, q.typeFlag.eq(typeFlag));
        predicate = ExpressionUtils.and(predicate, q.linkId.eq(id));
        predicate = ExpressionUtils.and(predicate, q.status.eq(Constant.STATUSDB));

        List<ApTodoEntity> apTodoEntities = jpaQueryFactory.selectFrom(q).
                where(predicate).
                orderBy(q.createPerson.desc()).fetch();
        if(apTodoEntities!=null&&apTodoEntities.size()>0){
             apTodoEntity= apTodoEntities.get(0);
        }
        return apTodoEntity;
    }


    @Override
    public void update(ApTodoEntity apTodoEntity) {

    }

    @Override
    public List<ApTodoEntity> queryTodoByLocation() {
        QApTodoEntity qApTodoEntity = QApTodoEntity.apTodoEntity;
        List<ApTodoEntity> apTodoEntitys = apTodoMapper.queryTodoByLocation(Constant.STATUSDB,Constant.NO_DELETE,
                "河南省",Constant.TYPRFLAGSS,"11",Constant.TYPEFLAGZC,"111");
        return apTodoEntitys;
    }

    @Override
    public void saveDB(ApTodoEntity apTodoEntity) {
        if (apTodoEntity ==null){
            throw new RuntimeException("新增不能为空");
        }
        apTodoEntity.setCreateTime(new Date());
       // apTodoEntity.setDelFlag(Constant.NO_DELETE);
        apTodoEntity.setStatus(Constant.STATUSDB);
        apTodoMapper.save(apTodoEntity);
    }
}
